package edu.gmu.projects.fireresponder.gui;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import edu.gmu.projects.fireresponder.R;
import edu.gmu.projects.fireresponder.environment.Location;

public class AssetDialogGalleryTab extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_view);

		Gallery gallery;
		ImageView imageView;

		Bundle bundle = getIntent().getExtras();
		double lat = bundle.getDouble(SituationMap.LAT);
		double lng = bundle.getDouble(SituationMap.LNG);

		String sdcard = Environment.getExternalStorageDirectory().getPath();
		String subdir = "images/frapp/"
				+ FileUtilities.santitizeToFileName(new Location(lat, lng)
						.toString());

		List<Drawable> drawables = getDrawablesFromPath(sdcard + "/" + subdir);

		imageView = (ImageView) findViewById(R.id.mImageView);

		if (drawables.size() > 0) {
			imageView.setImageDrawable(drawables.get(0));
		}

		gallery = (Gallery) findViewById(R.id.gallery);

		gallery.setAdapter(new ImageAdapter(this, drawables));

		gallery.setOnItemClickListener(new OnImageItemClickListener(imageView,
				drawables));

		registerForContextMenu(gallery);
	}

	private List<Drawable> getDrawablesFromPath(String path) {
		List<Drawable> drawables = new ArrayList<Drawable>();
		File dir = new File(path);

		File[] files = dir.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String filename) {
				return filename.endsWith(".jpg") || filename.endsWith(".jpeg")
						|| filename.endsWith(".png");
			}
		});

		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				drawables.add(Drawable.createFromPath(file.getPath()));
			}
		}

		return drawables;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add("Hello world");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
				.show();
		return true;
	}

	private class OnImageItemClickListener implements OnItemClickListener {
		private ImageView imageView;
		private List<Drawable> drawables;

		public OnImageItemClickListener(ImageView imageView,
				List<Drawable> drawables) {
			this.imageView = imageView;
			this.drawables = drawables;
		}

		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			imageView.setImageDrawable(drawables.get(position));
		}

	}

	private class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private List<Drawable> drawables;
		int mGalleryItemBackground;

		public ImageAdapter(Context c, List<Drawable> drawables) {
			this.mContext = c;
			this.drawables = drawables;

			// ---setting the style---
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() {
			return drawables.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(mContext);

			imageView.setImageDrawable(drawables.get(position));
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageView.setLayoutParams(new Gallery.LayoutParams(100, 80));
			imageView.setBackgroundResource(mGalleryItemBackground);

			return imageView;
		}
	}
}